<?php
	/**
	 * 购物车控制器
	 */
	class CartAction extends Action{

		//购物车视图
		public function index(){
			$_SESSION['cart'] = array_values($_SESSION['cart']);
			buyCartStatistics();
			$this->data = $_SESSION['cart'];
			p($_SESSION);
			$this->display();
		}

		//处理加入购物车的货品
		//异步处理购物车中的商品总数量及价格
		public function goodsToCart(){
			if($this->isAjax()){
				$specIds = $this->_post('goodsSpecIds');
				$gid = $this->_post('gid');
				$attrIds = explode(',', ltrim($specIds ,','));
				$buyNumber = $this->_post('number','intval');
				$db = D('CartCont');
				$attr = $attrIds[0] . '|' . $attrIds[1];
				$where = 'goods.id = ' . $gid . ' and ' . 'goods_list.attr="'.$attr.'"' .'and goods_attr.id in('. $attrIds[0] .','. $attrIds[1] .')';
 				$result = $db->where($where)->select();
 				if(is_array($result)){
 					$gsSumPrice = $result[0]['sprice'];
 					foreach($result as $key=>$val){
	 					$gsSumPrice += (float)$val['price'];
	 					$spec[] = $val['value'];
 					}
 					$cartGsImgShow = explode('|', $result[0]['medium']);
	 				$usergoodsInfo = array(
	 					'gid'=>$result[0]['id'],
	 					'gname'=>$result[0]['name'],
	 					'gimg'=>$cartGsImgShow[0],
	 					'sprice'=>$gsSumPrice,
	 					'unit'=>$result[0]['unit'],
	 					'sprice'=>number_format($gsSumPrice , '2'),
	 					'number'=>$buyNumber,
	 					'totalPrice' =>number_format($buyNumber*$gsSumPrice ,'2'),
	 					'color'=>$spec[0],
	 					'size'=>$spec[1],
	 					'spec'=>$specIds,
	 					'inventory'=>$result[0]['inventory']
	 				);
	 				session_start();
					//session保存一天
					$lifeTime = 24 * 3600;
					setcookie(session_name(), session_id(), time() + $lifeTime, "/");
 					$_SESSION['cart'][] =$usergoodsInfo;
 					//ajax from cart
	 				if($this->_post('where') == 'cart'){
	 					//buycart goods statistics
						buyCartStatistics('cart');
						$cartGsInfo = array(
							'totalPrice'=>number_format($gsSumPrice*$buyNumber,'2'),
							'inventory'=>$usergoodsInfo['inventory']
							);
	 					return $cartGsInfo;
	 				}else{
	 					//buycart goods statistics
						buyCartStatistics('gscpage');
						//this data use for buycartshow
						$cartUseGsInfo = array();
						foreach($_SESSION['cart'] as $key=>$val){
							$cartUseGsInfo[$key] = array(
										'gid' => $val['gid'],
										'gimg' => $val['gimg'],
										'gname' => $val['gname'],
										'sprice' => $val['sprice'],
										'number' => $val['number'],
										'color' => $val['color'],
										'size' => $val['size']
										);
						}
	 					echo json_encode(array('status'=>'ok' , 'cartGsNum'=>$_SESSION['cartGsNum'] , 'cartUseGsInfo'=>$cartUseGsInfo));
	 				}
 				}else{
					echo json_encode(array('status'=>'er'));
				}
			}
		}
		//异步处理购物车中的商品总数量及价格
		public function editBuyNum(){
			if($this->isAjax()){
				echo json_encode($this->goodsToCart());
			}else{
				echo json_encode(array('status'=>'er'));
			}
		}

		//ajax delete buycart's goods
		public function delCartGoods(){
			if($this->isAjax()){
				$cartGsNumF = count($_SESSION['cart']);
				$clid = $this->_post('clid','intval');
				unset($_SESSION['cart'][$clid]);
				$cartGsNumT = count($_SESSION['cart']);
				if($cartGsNumF > $cartGsNumT){
					$cartUseGsNum = 0;
					foreach($_SESSION['cart'] as $val){
						$cartUseGsNum += (int)$val['number'];
					}
					echo json_encode(array('status'=>'ok','cartGsNumT'=>$cartUseGsNum));
				}else{
					echo json_encode(array('status'=>'er'));
				}
			}
		}
	}
?>
